const App = getApp()

Page({
    data: {
        address: {
          items: [
            {
              _id:1,
              name:'罗翔',
              gender:'female',
              tel:'15757161464',
              address:'浙江省杭州市下沙高教园998号',
              is_def:1
            },
            {
              _id:2,
              name: '罗翔',
              gender: 'female',
              tel: '15757161464',
              address: '浙江省杭州市下沙高教园990号',
              is_def: 0
            }
          ],
          total:2
        },
        prompt: {
            hidden: !0,
            icon: '../../../image/iconfont-addr-empty.png',
            title: '还没有收货地址呢',
            text: '暂时没有相关数据',
        },
    },
    onLoad() {
        //this.address = App.HttpResource('/address/:id', {id: '@id'})
        //this.onPullDownRefresh()
    },
    initData() {
        this.setData({
            address: {
                items: [],
                params: {
                    page : 1,
                    limit: 10,
                },
                paginate: {}
            }
        })
    },
    toAddressEdit(e) {
        console.log(e)
        App.WxService.navigateTo('/pages/address/edit/index', {
            id: e.currentTarget.dataset.id
        })
    },
    toAddressAdd(e) {
        console.log(e)
        App.WxService.navigateTo('/pages/address/add/index')
    },
    setDefalutAddress(e) {
        const id = e.currentTarget.dataset.id
        App.HttpService.setDefalutAddress(id)
        .then(res => {
            const data = res.data
            console.log(data)
            if (data.meta.code == 0) {
                this.onPullDownRefresh()
            }
        })
    },
    getList() {
        const address = this.data.address
        const params = address.params

        // App.HttpService.getAddressList(params)
        // this.address.queryAsync(params)
        // .then(res => {
        //     const data = res.data
        //     console.log(data)
        //     if (data.meta.code == 0) {
        //         address.items = [...address.items, ...data.data.items]
        //         address.paginate = data.data.paginate
        //         address.params.page = data.data.paginate.next
        //         address.params.limit = data.data.paginate.perPage
        //         this.setData({
        //             address: address,
        //             'prompt.hidden': address.items.length,
        //         })
        //     }
        // })
    },
    onPullDownRefresh() {
        console.info('onPullDownRefresh')
       // this.initData()
       // this.getList()
    },
    onReachBottom() {
        console.info('onReachBottom')
        if (!this.data.address.paginate.hasNext) return
        this.getList()
    },
})